package com.itheima.web;

import com.itheima.pojo.User;
import com.itheima.service.Service;
import com.itheima.service.ServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/registerServlet")
public class RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");

        String username = request.getParameter("username");
        String pwd = request.getParameter("pwd");
        User user = new User();
        user.setUsername(username);
        user.setPassword(pwd);

        Service serviceImpl = new ServiceImpl();

        User u= serviceImpl.registerUser(user);

        if (u == null) {
            response.getWriter().print(false);
            int i =serviceImpl.addUser(user);
        } else {
            response.getWriter().print(true);
        }



    }
}